<template><div><h2 id="数据库-迁移" tabindex="-1"><a class="header-anchor" href="#数据库-迁移"><span>数据库: 迁移</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#generating-migrations">生成迁移</a>
<ul>
<li><a href="#squashing-migrations">整合迁移</a></li>
</ul>
</li>
<li><a href="#migration-structure">迁移结构</a></li>
<li><a href="#running-migrations">执行迁移</a>
<ul>
<li><a href="#rolling-back-migrations">回滚迁移</a></li>
</ul>
</li>
<li><a href="#tables">数据表</a>
<ul>
<li><a href="#creating-tables">创建数据表</a></li>
<li><a href="#updating-tables">更新数据表</a></li>
<li><a href="#renaming-and-dropping-tables">重命名 / 删除表</a></li>
</ul>
</li>
<li><a href="#columns">字段</a>
<ul>
<li><a href="#creating-columns">创建字段</a></li>
<li><a href="#available-column-types">可用的字段类型</a></li>
<li><a href="#column-modifiers">字段修饰符</a></li>
<li><a href="#modifying-columns">修改字段</a></li>
<li><a href="#renaming-columns">重命名字段</a></li>
<li><a href="#dropping-columns">删除字段</a></li>
</ul>
</li>
<li><a href="#indexes">索引</a>
<ul>
<li><a href="#creating-indexes">创建索引</a></li>
<li><a href="#renaming-indexes">重命名索引</a></li>
<li><a href="#dropping-indexes">删除索引</a></li>
<li><a href="#foreign-key-constraints">外键约束</a></li>
</ul>
</li>
<li><a href="#events">事件</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>迁移就像数据库的版本控制，允许你的团队定义和共享应用程序的数据库架构定义。 如果你曾经不得不告诉团队成员在从代码控制中拉取更新后手动添加字段到他们的本地数据库，那么你就遇到了数据库迁移解决的问题。</p>
<p>Laravel <code v-pre>Schema</code> <a href="https://learnku.com/docs/laravel/10.x/facades" target="_blank" rel="noopener noreferrer">facade</a> 为所有 Laravel 支持的数据库系统的创建和操作表提供了不依赖于数据库的支持。通常情况下，迁移会使用 facade 来创建和修改数据表和字段。</p>
<h2 id="生成迁移" tabindex="-1"><a class="header-anchor" href="#生成迁移"><span>生成迁移</span></a></h2>
<p>你可以使用 <code v-pre>make:migration</code> <a href="https://learnku.com/docs/laravel/10.x/artisan" target="_blank" rel="noopener noreferrer">Artisan 命令</a> 来生成数据库迁移。新的迁移文件将放在你的 <code v-pre>database/migrations</code> 目录下。每个迁移文件名都包含一个时间戳来使 Laravel 确定迁移的顺序：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:migration create_flights_table</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Laravel 将使用迁移文件的名称来猜测表名以及迁移是否会创建一个新表。如果 Laravel 能够从迁移文件的名称中确定表的名称，它将在生成的迁移文件中预填入指定的表，或者，你也可以直接在迁移文件中手动指定表名。</p>
<p>如果要为生成的迁移指定自定义路径，你可以在执行 <code v-pre>make:migration</code> 命令时使用 <code v-pre>--path</code> 选项。给定的路径应该相对于应用程序的基本路径。</p>
<blockquote>
<p><strong>技巧</strong><br>
可以使用 <a href="https://learnku.com/docs/laravel/10.x/artisanmd#stub-customization" target="_blank" rel="noopener noreferrer">stub publishing</a> 自定义发布。</p>
</blockquote>
<h3 id="整合迁移" tabindex="-1"><a class="header-anchor" href="#整合迁移"><span>整合迁移</span></a></h3>
<p>在构建应用程序时，可能会随着时间的推移积累越来越多的迁移。这可能会导致你的 <code v-pre>database/migrations</code> 目录因为数百次迁移而变得臃肿。你如果愿意的话，可以将迁移「压缩」到单个 SQL 文件中。如果你想这样做，请先执行<code v-pre>schema:dump</code> 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan schema:dump</span>
<span class="line"></span>
<span class="line"><span class="token comment"># 转储当前数据库架构并删除所有现有迁移...</span></span>
<span class="line">php artisan schema:dump <span class="token parameter variable">--prune</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>执行此命令时，Laravel 将向应用程序的 <code v-pre>database/schema</code> 目录写入一个「schema」文件。现在，当你尝试迁移数据库而没有执行其他迁移时，Laravel 将首先执行模式文件的 SQL 语句。在执行数据库结构文件的语句之后，Laravel 将执行不属于数据库结构的剩余的所有迁移。</p>
<p>如果你的应用程序的测试使用的数据库连接与你在本地开发过程中通常使用的不同，你应该确保你已经使用该数据库连接转储了一个 schema 文件，以便你的测试能够建立你的数据库。你可能希望在切换（dump）你在本地开发过程中通常使用的数据库连接之后再做这件事。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan schema:dump</span>
<span class="line">php artisan schema:dump <span class="token parameter variable">--database</span><span class="token operator">=</span>testing <span class="token parameter variable">--prune</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>你应该将数据库模式文件提交给源代码管理，以便团队中的其他新开发人员可以快速创建应用程序的初始数据库结构。</p>
<blockquote>
<p><strong>注意</strong><br>
整合迁移仅适用于 MySQL、PostgreSQL 和 SQLite 数据库，并使用数据库命令行的客户端。另外，数据库结构不能还原到内存中的 SQLite 数据库。</p>
</blockquote>
<h2 id="迁移结构" tabindex="-1"><a class="header-anchor" href="#迁移结构"><span>迁移结构</span></a></h2>
<p>迁移类包含两个方法：<code v-pre>up</code> 和 <code v-pre>down</code> 。<code v-pre>up</code> 方法用于向数据库中添加新表、列或索引，而 down 方法用于撤销 <code v-pre>up</code> 方法执行的操作。.</p>
<p>在这两种方法中，可以使用 Laravel 模式构建器来富有表现力地创建和修改表。要了解 <code v-pre>Schema</code> 构建器上可用的所有方法，<a href="#creating-tables">查看其文档</a>。例如，以下迁移会创建一个 <code v-pre>flights</code> 表：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Migrations<span class="token punctuation">\</span>Migration</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token keyword">class</span> <span class="token class-name-definition class-name">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 执行迁移</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'flights'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'airline'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamps</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 回滚迁移</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">down</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">drop</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'flights'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="设置迁移连接" tabindex="-1"><a class="header-anchor" href="#设置迁移连接"><span>设置迁移连接</span></a></h4>
<p>如果你的迁移将与应用程序默认数据库连接以外的数据库连接进行交互，你应该设置迁移的 <code v-pre>$connection</code> 属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * The database connection that should be used by the migration.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$connection</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'pgsql'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 执行迁移</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="执行迁移" tabindex="-1"><a class="header-anchor" href="#执行迁移"><span>执行迁移</span></a></h2>
<p>执行 Artisan 命令 <code v-pre>migrate</code>，来运行所有未执行过的迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你想查看目前已经执行了哪些迁移，可以使用 <code v-pre>migrate:status</code> Artisan 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:status</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你希望在不实际运行迁移的情况下看到将被执行的SQL语句，你可以在 <code v-pre>migrate</code> 命令中提供 <code v-pre>--pretend</code> 选项。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate <span class="token parameter variable">--pretend</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="在隔离的环境中执行迁移" tabindex="-1"><a class="header-anchor" href="#在隔离的环境中执行迁移"><span>在隔离的环境中执行迁移</span></a></h4>
<p>如果你在多个服务器上部署你的应用程序，并在部署过程中运行迁移，你可能不希望两个服务器同时尝试迁移数据库。为了避免这种情况，你可以在调用 <code v-pre>migrate</code> 命令时使用 <code v-pre>isolated</code> 选项。</p>
<p>当提供 <code v-pre>isolated</code> 选项时, Laravel 将使用你的应用程序缓存驱动获得一个原子锁，然后再尝试运行你的迁移。所有其他试图运行 <code v-pre>migrate</code> 命令的尝试在锁被持有时都不会执行; 然而, 命令仍然会以成功的退出状态码退出:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate <span class="token parameter variable">--isolated</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
要使用这个功能，你的应用程序必须使用 <code v-pre>memcached</code> / <code v-pre>redis</code> / <code v-pre>dynamodb</code> / <code v-pre>database</code> / <code v-pre>file</code> 或 <code v-pre>array</code> 缓存驱动作为你应用程序的默认缓存驱动。此外，所有的服务器必须与同一个中央缓存服务器进行通信。</p>
</blockquote>
<h4 id="在生产环境中执行强制迁移" tabindex="-1"><a class="header-anchor" href="#在生产环境中执行强制迁移"><span>在生产环境中执行强制迁移</span></a></h4>
<p>有些迁移操作是破坏性的，这意味着它们可能会导致数据丢失。为了防止你对生产数据库运行这些命令，在执行这些命令之前，系统将提示你进行确认。如果要在运行强制命令的时候去掉提示，需要加上 <code v-pre>--force</code> 标志：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate <span class="token parameter variable">--force</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="回滚迁移" tabindex="-1"><a class="header-anchor" href="#回滚迁移"><span>回滚迁移</span></a></h3>
<p>如果要回滚最后一次迁移操作，可以使用 Artisan 命令 <code v-pre>rollback</code>。该命令会回滚最后「一批」的迁移，这可能包含多个迁移文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:rollback</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>通过向 <code v-pre>rollback</code> 命令加上 <code v-pre>step</code> 参数，可以回滚指定数量的迁移。例如，以下命令将回滚最后五个迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:rollback <span class="token parameter variable">--step</span><span class="token operator">=</span><span class="token number">5</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过向 <code v-pre>rollback</code> 命令提供 <code v-pre>batch</code> 选项来回滚特定的批次迁移，其中 <code v-pre>batch</code> 选项对应于应用程序中 <code v-pre>migrations</code> 数据库表中的一个批次值。例如，下面的命令将回滚第三批中的所有迁移。</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:rollback <span class="token parameter variable">--batch</span><span class="token operator">=</span><span class="token number">3</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>命令 <code v-pre>migrate:reset</code> 会回滚应用已运行过的所有迁移：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:reset</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="使用单个命令同时进行回滚和迁移操作" tabindex="-1"><a class="header-anchor" href="#使用单个命令同时进行回滚和迁移操作"><span>使用单个命令同时进行回滚和迁移操作</span></a></h4>
<p>命令 <code v-pre>migrate:refresh</code> 首先会回滚已运行过的所有迁移，随后会执行 <code v-pre>migrate</code>。这一命令可以高效地重建你的整个数据库：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:refresh</span>
<span class="line"></span>
<span class="line"><span class="token comment"># 重置数据库，并运行所有的 seeds...</span></span>
<span class="line">php artisan migrate:refresh <span class="token parameter variable">--seed</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通过在命令 <code v-pre>refresh</code> 中使用 <code v-pre>step</code> 参数，你可以回滚并重新执行指定数量的迁移操作。例如，下列命令会回滚并重新执行最后五个迁移操作：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:refresh <span class="token parameter variable">--step</span><span class="token operator">=</span><span class="token number">5</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="删除所有表然后执行迁移" tabindex="-1"><a class="header-anchor" href="#删除所有表然后执行迁移"><span>删除所有表然后执行迁移</span></a></h4>
<p>命令 <code v-pre>migrate:fresh</code> 会删去数据库中的所有表，随后执行命令 <code v-pre>migrate</code>：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan migrate:fresh</span>
<span class="line"></span>
<span class="line">php artisan migrate:fresh <span class="token parameter variable">--seed</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
该命令 <code v-pre>migrate:fresh</code> 在删去所有数据表的过程中，会无视它们的前缀。如果数据库涉及到其它应用，使用该命令须十分小心。</p>
</blockquote>
<h2 id="数据表" tabindex="-1"><a class="header-anchor" href="#数据表"><span>数据表</span></a></h2>
<h3 id="创建数据表" tabindex="-1"><a class="header-anchor" href="#创建数据表"><span>创建数据表</span></a></h3>
<p>接下来我们将使用 Schema 的 <code v-pre>create</code> 方法创建一个新的数据表。<code v-pre>create</code> 接受两个参数：第一个参数是表名，而第二个参数是一个闭包，该闭包接受一个用来定义新数据表的 <code v-pre>Blueprint</code> 对象：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamps</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>创建表时，可以使用数据库结构构建器的 <a href="#creating-columns">列方法</a> 来定义表的列。</p>
<h4 id="检查表-列是否存在" tabindex="-1"><a class="header-anchor" href="#检查表-列是否存在"><span>检查表 / 列是否存在</span></a></h4>
<p>你可以使用 <code v-pre>hasTable</code> 和 <code v-pre>hasColumn</code> 方法检查表或列是否存在：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">hasTable</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 「users」表存在...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">hasColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 「users」表存在，并且有「email」列...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="数据库连接和表选项" tabindex="-1"><a class="header-anchor" href="#数据库连接和表选项"><span>数据库连接和表选项</span></a></h4>
<p>如果要对不是应用程序默认的数据库连接执行数据库结构的操作，请使用 <code v-pre>connection</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">connection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sqlite'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，还可以使用其他一些属性和方法来定义表创建的其他地方。使用 MySQL 时，可以使用 <code v-pre>engine</code> 属性指定表的存储引擎：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token property">engine</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'InnoDB'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>charset</code> 和 <code v-pre>collation</code> 属性可用于在使用 MySQL 时为创建的表指定字符集和排序规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token property">charset</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'utf8mb4'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token property">collation</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'utf8mb4_unicode_ci'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>temporary</code> 方法可用于将表标识为「临时」状态。临时表仅对当前连接的数据库会话可见，当连接关闭时会自动删除：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'calculations'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">temporary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想给数据库表添加「注释」，你可以在表实例上调用<code v-pre>comment</code>方法。目前只有 MySQL 和 Postgres 支持表注释：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'calculations'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">comment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Business calculations'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="更新数据表" tabindex="-1"><a class="header-anchor" href="#更新数据表"><span>更新数据表</span></a></h3>
<p><code v-pre>Schema</code> 门面的 <code v-pre>table</code> 方法可用于更新现有表。与 <code v-pre>create</code> 方法一样，<code v-pre>table</code> 方法接受两个参数：表的名称和接收可用于向表添加列或索引的 <code v-pre>Blueprint</code> 实例的闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="重命名-删除表" tabindex="-1"><a class="header-anchor" href="#重命名-删除表"><span>重命名 / 删除表</span></a></h3>
<p>要重命名已存在的数据表，使用 <code v-pre>rename</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">rename</span><span class="token punctuation">(</span><span class="token variable">$from</span><span class="token punctuation">,</span> <span class="token variable">$to</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要删除已存在的表，你可以使用 <code v-pre>drop</code> 或 <code v-pre>dropIfExists</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">drop</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">dropIfExists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="使用外键重命名表" tabindex="-1"><a class="header-anchor" href="#使用外键重命名表"><span>使用外键重命名表</span></a></h4>
<p>在重命名表之前，应该确认表的所有外键约束在迁移文件中有一个显式的名称，而不是让 Laravel 去指定。否则，外键约束名称将引用旧表名。</p>
<h2 id="字段" tabindex="-1"><a class="header-anchor" href="#字段"><span>字段</span></a></h2>
<h3 id="创建字段" tabindex="-1"><a class="header-anchor" href="#创建字段"><span>创建字段</span></a></h3>
<p>门面 <code v-pre>Schema</code> 的 <code v-pre>table</code> 方法可用于更新表。与 <code v-pre>create</code> 方法一样， <code v-pre>table</code> 方法接受两个参数：表名和一个闭包，该闭包接收一个 <code v-pre>illumb\Database\Schema\Blueprint</code> 实例，可以使用该实例向表中添加列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="可用的字段类型" tabindex="-1"><a class="header-anchor" href="#可用的字段类型"><span>可用的字段类型</span></a></h3>
<p>Schema 构建器 <code v-pre>Illuminate\Database\Schema\Blueprint</code> 提供了多种方法，用来创建表中对应类型的列。下面列出了所有可用的方法：</p>
<h4 id="bigincrements-collection-method-first-collection-method" tabindex="-1"><a class="header-anchor" href="#bigincrements-collection-method-first-collection-method"><span><code v-pre>bigIncrements()</code> {.collection-method .first-collection-method}</span></a></h4>
<p><code v-pre>bigIncrements</code> 方法用于在数据表中创建一个自增的 <code v-pre>UNSIGNED BIGINT</code> 类型（主键）的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">bigIncrements</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="biginteger-collection-method" tabindex="-1"><a class="header-anchor" href="#biginteger-collection-method"><span><code v-pre>bigInteger()</code> {.collection-method}</span></a></h4>
<p><code v-pre>bigInteger</code> 方法用于在数据表中创建一个 <code v-pre>BIGINT</code> 类型的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">bigInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="binary-collection-method" tabindex="-1"><a class="header-anchor" href="#binary-collection-method"><span><code v-pre>binary()</code> {.collection-method}</span></a></h4>
<p><code v-pre>binary</code> 方法用于在数据表中创建一个 <code v-pre>BLOB</code> 类型的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">binary</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="boolean-collection-method" tabindex="-1"><a class="header-anchor" href="#boolean-collection-method"><span><code v-pre>boolean()</code> {.collection-method}</span></a></h4>
<p><code v-pre>boolean</code> 方法用于在数据表中创建一个 <code v-pre>BOOLEAN</code> 类型的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">boolean</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'confirmed'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="char-collection-method" tabindex="-1"><a class="header-anchor" href="#char-collection-method"><span><code v-pre>char()</code> {.collection-method}</span></a></h4>
<p><code v-pre>char</code> 方法用于在数据表中创建一个 <code v-pre>CHAR</code> 类型的列，长度由参数指定：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">char</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="datetimetz-collection-method" tabindex="-1"><a class="header-anchor" href="#datetimetz-collection-method"><span><code v-pre>dateTimeTz()</code> {.collection-method}</span></a></h4>
<p><code v-pre>dateTimeTz</code> 方法用于在数据表中创建一个 <code v-pre>DATETIME</code> 类型（附有 timezone）的列，可选参数为精度的总位数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dateTimeTz</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span> <span class="token variable">$precision</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="datetime-collection-method" tabindex="-1"><a class="header-anchor" href="#datetime-collection-method"><span><code v-pre>dateTime()</code> {.collection-method}</span></a></h4>
<p><code v-pre>dateTime</code> 方法用于在数据表中创建一个 <code v-pre>DATETIME</code> 类型的列，可选参数为精度的总位数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dateTime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span> <span class="token variable">$precision</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="date-collection-method" tabindex="-1"><a class="header-anchor" href="#date-collection-method"><span><code v-pre>date()</code> {.collection-method}</span></a></h4>
<p><code v-pre>date</code> 方法用于在数据表中创建一个 <code v-pre>DATE</code> 类型的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">date</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="decimal-collection-method" tabindex="-1"><a class="header-anchor" href="#decimal-collection-method"><span><code v-pre>decimal()</code> {.collection-method}</span></a></h4>
<p><code v-pre>decimal</code> 方法用于在数据表中创建一个 <code v-pre>DECIMAL</code> 类型的列，可选参数分别为有效字数总位数、小数部分总位数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">decimal</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'amount'</span><span class="token punctuation">,</span> <span class="token variable">$precision</span> <span class="token operator">=</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token variable">$scale</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="double-collection-method" tabindex="-1"><a class="header-anchor" href="#double-collection-method"><span><code v-pre>double()</code> {.collection-method}</span></a></h4>
<p><code v-pre>double</code> 方法用于在数据表中创建一个 <code v-pre>DOUBLE</code> 类型的列，可选参数分别为有效字数总位数、小数部分总位数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">double</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'amount'</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="enum-collection-method" tabindex="-1"><a class="header-anchor" href="#enum-collection-method"><span><code v-pre>enum()</code> {.collection-method}</span></a></h4>
<p><code v-pre>enum</code> 方法用于在数据表中创建一个 <code v-pre>ENUM</code> 类型的列，合法的值列表由参数指定：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">enum</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'difficulty'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'easy'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'hard'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="float-collection-method" tabindex="-1"><a class="header-anchor" href="#float-collection-method"><span><code v-pre>float()</code> {.collection-method}</span></a></h4>
<p><code v-pre>float</code> 方法用于在数据表中创建一个 <code v-pre>FLOAT</code> 类型的列，可选参数分别为有效字数总位数、小数部分总位数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">float</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'amount'</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="foreignid-collection-method" tabindex="-1"><a class="header-anchor" href="#foreignid-collection-method"><span><code v-pre>foreignId()</code> {.collection-method}</span></a></h4>
<p><code v-pre>foreignId</code> 方法是 <code v-pre>unsignedBigInteger</code> 的别名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignId</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="foreignidfor-collection-method" tabindex="-1"><a class="header-anchor" href="#foreignidfor-collection-method"><span><code v-pre>foreignIdFor()</code> {.collection-method}</span></a></h4>
<p><code v-pre>foreignIdFor</code> 方法为给定模型类添加了 <code v-pre>{column}_id UNSIGNED BIGINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignIdFor</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="foreignulid-collection-method" tabindex="-1"><a class="header-anchor" href="#foreignulid-collection-method"><span><code v-pre>foreignUlid()</code> {.collection-method}</span></a></h4>
<p><code v-pre>foreignUlid</code> 方法创建一个 <code v-pre>ULID</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignUlid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="foreignuuid-collection-method" tabindex="-1"><a class="header-anchor" href="#foreignuuid-collection-method"><span><code v-pre>foreignUuid()</code> {.collection-method}</span></a></h4>
<p><code v-pre>foreignUuid</code> 方法创建一个 <code v-pre>UUID</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignUuid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="geometrycollection-collection-method" tabindex="-1"><a class="header-anchor" href="#geometrycollection-collection-method"><span><code v-pre>geometryCollection()</code> {.collection-method}</span></a></h4>
<p><code v-pre>geometryCollection</code> 方法相当于 <code v-pre>GEOMETRYCOLLECTION</code> :</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">geometryCollection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'positions'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="geometry-collection-method" tabindex="-1"><a class="header-anchor" href="#geometry-collection-method"><span><code v-pre>geometry()</code> {.collection-method}</span></a></h4>
<p><code v-pre>geometry</code> 方法相当于 <code v-pre>GEOMETRY</code> :</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">geometry</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'positions'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="id-collection-method" tabindex="-1"><a class="header-anchor" href="#id-collection-method"><span><code v-pre>id()</code> {.collection-method}</span></a></h4>
<p><code v-pre>id</code> 方法是<code v-pre>bigIncrements</code> 的别名。默认情况下，该方法将创建一个 <code v-pre>id</code> 列; 但是，如果要为列指定不同的名称，则可以传递列名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="increments-collection-method" tabindex="-1"><a class="header-anchor" href="#increments-collection-method"><span><code v-pre>increments()</code> {.collection-method}</span></a></h4>
<p><code v-pre>increments</code> 方法创建一个自动递增相当于 <code v-pre>UNSIGNED INTEGER</code> 的列作为主键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">increments</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="integer-collection-method" tabindex="-1"><a class="header-anchor" href="#integer-collection-method"><span><code v-pre>integer()</code> {.collection-method}</span></a></h4>
<p><code v-pre>integer</code> 方法相当于 <code v-pre>INTEGER</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="ipaddress-collection-method" tabindex="-1"><a class="header-anchor" href="#ipaddress-collection-method"><span><code v-pre>ipAddress()</code> {.collection-method}</span></a></h4>
<p><code v-pre>ipAddress</code> 方法相当于 <code v-pre>VARCHAR</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">ipAddress</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'visitor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="json-collection-method" tabindex="-1"><a class="header-anchor" href="#json-collection-method"><span><code v-pre>json()</code> {.collection-method}</span></a></h4>
<p><code v-pre>json</code> 方法相当于 <code v-pre>JSON</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'options'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="jsonb-collection-method" tabindex="-1"><a class="header-anchor" href="#jsonb-collection-method"><span><code v-pre>jsonb()</code> {.collection-method}</span></a></h4>
<p><code v-pre>jsonb</code> 方法相当于 <code v-pre>JSONB</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">jsonb</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'options'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="linestring-collection-method" tabindex="-1"><a class="header-anchor" href="#linestring-collection-method"><span><code v-pre>lineString()</code> {.collection-method}</span></a></h4>
<p><code v-pre>lineString</code> 方法相当于 <code v-pre>LINESTRING</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">lineString</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'positions'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="longtext-collection-method" tabindex="-1"><a class="header-anchor" href="#longtext-collection-method"><span><code v-pre>longText()</code> {.collection-method}</span></a></h4>
<p><code v-pre>longText</code> 方法相当于 <code v-pre>LONGTEXT</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">longText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="macaddress-collection-method" tabindex="-1"><a class="header-anchor" href="#macaddress-collection-method"><span><code v-pre>macAddress()</code> {.collection-method}</span></a></h4>
<p><code v-pre>macAddress</code> 方法创建一个用于保存 MAC 地址的列。一些数据库系统（如 PostgreSQL），为这种类型的数据提供了专用的类型。其他数据库系统相当于使用字符串类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">macAddress</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'device'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="mediumincrements-collection-method" tabindex="-1"><a class="header-anchor" href="#mediumincrements-collection-method"><span><code v-pre>mediumIncrements()</code> {.collection-method}</span></a></h4>
<p><code v-pre>mediumIncrements</code> 方法用于创建一个 <code v-pre>UNSIGNED MEDIUMINT</code> 类型的自动递增的列作为主键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">mediumIncrements</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="mediuminteger-collection-method" tabindex="-1"><a class="header-anchor" href="#mediuminteger-collection-method"><span><code v-pre>mediumInteger()</code> {.collection-method}</span></a></h4>
<p><code v-pre>mediumInteger</code> 方法用于创建一个 <code v-pre>MEDIUMINT</code> 类型的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">mediumInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="mediumtext-collection-method" tabindex="-1"><a class="header-anchor" href="#mediumtext-collection-method"><span><code v-pre>mediumText()</code> {.collection-method}</span></a></h4>
<p><code v-pre>mediumText</code> 方法用于创建一个 <code v-pre>MEDIUMTEXT</code> 类型的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">mediumText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="morphs-collection-method" tabindex="-1"><a class="header-anchor" href="#morphs-collection-method"><span><code v-pre>morphs()</code> {.collection-method}</span></a></h4>
<p><code v-pre>morphs</code> 方法用于快速创建一个名称为 <code v-pre>{column}_id</code> ，类型为 <code v-pre>UNSIGNED BIGINT</code> 的列和一个名称为 <code v-pre>{column}_type</code> ，类型为 <code v-pre>VARCHAR</code> 的列。</p>
<p>这个方法在定义<a href="https://learnku.com/docs/laravel/10.x/eloquent-relationships" target="_blank" rel="noopener noreferrer">多态关联</a>所需的列时使用。在下面的例子中， <code v-pre>taggable_id</code> 和 <code v-pre>taggable_type</code> 这两个列会被创建：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">morphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="multilinestring-collection-method" tabindex="-1"><a class="header-anchor" href="#multilinestring-collection-method"><span><code v-pre>multiLineString()</code> {.collection-method}</span></a></h4>
<p><code v-pre>multiLineString</code> 方法用于创建一个 <code v-pre>MULTILINESTRING</code> 类型的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">multiLineString</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'positions'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="multipoint-collection-method" tabindex="-1"><a class="header-anchor" href="#multipoint-collection-method"><span><code v-pre>multiPoint()</code> {.collection-method}</span></a></h4>
<p><code v-pre>multiPoint</code> 方法用于创建一个 <code v-pre>MULTIPOINT</code> 类型的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">multiPoint</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'positions'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="multipolygon-collection-method" tabindex="-1"><a class="header-anchor" href="#multipolygon-collection-method"><span><code v-pre>multiPolygon()</code> {.collection-method}</span></a></h4>
<p><code v-pre>multiPolygon</code> 方法用于创建一个 <code v-pre>MULTIPOLYGON</code> 类型的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">multiPolygon</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'positions'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="nullabletimestamps-collection-method" tabindex="-1"><a class="header-anchor" href="#nullabletimestamps-collection-method"><span><code v-pre>nullableTimestamps()</code> {.collection-method}</span></a></h4>
<p>这个方法和 <a href="#column-method-timestamps">timestamps</a> 方法类似；需要注意的是此方法创建的列是 <code v-pre>nullable</code> 的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">nullableTimestamps</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="nullablemorphs-collection-method" tabindex="-1"><a class="header-anchor" href="#nullablemorphs-collection-method"><span><code v-pre>nullableMorphs()</code> {.collection-method}</span></a></h4>
<p>这个方法和 <a href="#column-method-morphs">morphs</a> 方法类似；需要注意的是此方法创建的列是 <code v-pre>nullable</code> 的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">nullableMorphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="nullableulidmorphs-collection-method" tabindex="-1"><a class="header-anchor" href="#nullableulidmorphs-collection-method"><span><code v-pre>nullableUlidMorphs()</code> {.collection-method}</span></a></h4>
<p>这个方法和 <a href="#column-method-ulidMorphs">ulidMorphs</a> 方法类似；需要注意的是此方法创建的列是 <code v-pre>nullable</code>。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">nullableUlidMorphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="nullableuuidmorphs-collection-method" tabindex="-1"><a class="header-anchor" href="#nullableuuidmorphs-collection-method"><span><code v-pre>nullableUuidMorphs()</code> {.collection-method}</span></a></h4>
<p>这个方法和 <a href="#column-method-uuidMorphs">uuidMorphs</a> 方法类似；需要注意的是此方法创建的列是 <code v-pre>nullable</code> 的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">nullableUuidMorphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="point-collection-method" tabindex="-1"><a class="header-anchor" href="#point-collection-method"><span><code v-pre>point()</code> {.collection-method}</span></a></h4>
<p><code v-pre>point</code> 方法相当于 <code v-pre>POINT</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">point</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'position'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="polygon-collection-method" tabindex="-1"><a class="header-anchor" href="#polygon-collection-method"><span><code v-pre>polygon()</code> {.collection-method}</span></a></h4>
<p>The <code v-pre>polygon</code> method creates a <code v-pre>POLYGON</code> equivalent column:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">polygon</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'position'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="remembertoken-collection-method" tabindex="-1"><a class="header-anchor" href="#remembertoken-collection-method"><span><code v-pre>rememberToken()</code> {.collection-method}</span></a></h4>
<p>添加一个允许空值的 <code v-pre>VARCHAR (100)</code> 类型的 <code v-pre>remember_token</code> 字段，用于存储 <a href="https://learnku.com/docs/laravel/10.x/authentication#remembering-users" target="_blank" rel="noopener noreferrer">记住用户</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">rememberToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="set-collection-method" tabindex="-1"><a class="header-anchor" href="#set-collection-method"><span><code v-pre>set()</code> {.collection-method}</span></a></h4>
<p><code v-pre>set</code> 方法使用给定的有效值列表创建一个 <code v-pre>SET</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'flavors'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'strawberry'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'vanilla'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="smallincrements-collection-method" tabindex="-1"><a class="header-anchor" href="#smallincrements-collection-method"><span><code v-pre>smallIncrements()</code> {.collection-method}</span></a></h4>
<p><code v-pre>smallIncrements</code> 方法创建一个自动递增的 <code v-pre>UNSIGNED SMALLINT</code> 等效列作为主键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">smallIncrements</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="smallinteger-collection-method" tabindex="-1"><a class="header-anchor" href="#smallinteger-collection-method"><span><code v-pre>smallInteger()</code> {.collection-method}</span></a></h4>
<p><code v-pre>smallInteger</code> 方法创建一个 <code v-pre>SMALLINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">smallInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="softdeletestz-collection-method" tabindex="-1"><a class="header-anchor" href="#softdeletestz-collection-method"><span><code v-pre>softDeletesTz()</code> {.collection-method}</span></a></h4>
<p><code v-pre>softDeletesTz</code> 方法添加了一个可为空的 <code v-pre>deleted_at</code> <code v-pre>TIMESTAMP</code>（带时区）等效列，具有可选精度（总位数）。此列旨在存储 Eloquent 的“软删除”功能所需的 <code v-pre>deleted_at</code> 时间戳：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">softDeletesTz</span><span class="token punctuation">(</span><span class="token variable">$column</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'deleted_at'</span><span class="token punctuation">,</span> <span class="token variable">$precision</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="softdeletes-collection-method" tabindex="-1"><a class="header-anchor" href="#softdeletes-collection-method"><span><code v-pre>softDeletes()</code> {.collection-method}</span></a></h4>
<p><code v-pre>softDeletes</code> 方法添加了一个可为空的 <code v-pre>deleted_at</code> <code v-pre>TIMESTAMP</code> 等效列，具有可选精度（总位数）。此列旨在存储 Eloquent 的「软删除」功能所需的 <code v-pre>deleted_at</code> 时间戳，相当于为软删除添加一个可空的 <code v-pre>deleted_at</code> 字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">softDeletes</span><span class="token punctuation">(</span><span class="token variable">$column</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'deleted_at'</span><span class="token punctuation">,</span> <span class="token variable">$precision</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="string-collection-method" tabindex="-1"><a class="header-anchor" href="#string-collection-method"><span><code v-pre>string()</code> {.collection-method}</span></a></h4>
<p><code v-pre>string</code> 方法创建一个给定长度的 <code v-pre>VARCHAR</code> 等效列，相当于指定长度的 VARCHAR：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="text-collection-method" tabindex="-1"><a class="header-anchor" href="#text-collection-method"><span><code v-pre>text()</code> {.collection-method}</span></a></h4>
<p><code v-pre>text</code> 方法创建一个 <code v-pre>TEXT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'description'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="timetz-collection-method" tabindex="-1"><a class="header-anchor" href="#timetz-collection-method"><span><code v-pre>timeTz()</code> {.collection-method}</span></a></h4>
<p><code v-pre>timeTz</code> 方法创建一个具有可选精度（总位数）的 <code v-pre>TIME</code>（带时区）等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timeTz</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sunrise'</span><span class="token punctuation">,</span> <span class="token variable">$precision</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="time-collection-method" tabindex="-1"><a class="header-anchor" href="#time-collection-method"><span><code v-pre>time()</code> {.collection-method}</span></a></h4>
<p><code v-pre>time</code> 方法创建一个具有可选精度（总位数）的 <code v-pre>TIME</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">time</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sunrise'</span><span class="token punctuation">,</span> <span class="token variable">$precision</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="timestamptz-collection-method" tabindex="-1"><a class="header-anchor" href="#timestamptz-collection-method"><span><code v-pre>timestampTz()</code> {.collection-method}</span></a></h4>
<p><code v-pre>timestampTz</code> 方法创建一个具有可选精度（总位数）的 <code v-pre>TIMESTAMP</code>（带时区）等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestampTz</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'added_at'</span><span class="token punctuation">,</span> <span class="token variable">$precision</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="timestamp-collection-method" tabindex="-1"><a class="header-anchor" href="#timestamp-collection-method"><span><code v-pre>timestamp()</code> {.collection-method}</span></a></h4>
<p><code v-pre>timestamp</code> 方法创建一个具有可选精度（总位数）的 <code v-pre>TIMESTAMP</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamp</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'added_at'</span><span class="token punctuation">,</span> <span class="token variable">$precision</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="timestampstz-collection-method" tabindex="-1"><a class="header-anchor" href="#timestampstz-collection-method"><span><code v-pre>timestampsTz()</code> {.collection-method}</span></a></h4>
<p><code v-pre>timestampsTz</code> 方法创建 <code v-pre>created_at</code> 和 <code v-pre>updated_at</code> <code v-pre>TIMESTAMP</code>（带时区）等效列，具有可选精度（总位数）：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestampsTz</span><span class="token punctuation">(</span><span class="token variable">$precision</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="timestamps-collection-method" tabindex="-1"><a class="header-anchor" href="#timestamps-collection-method"><span><code v-pre>timestamps()</code> {.collection-method}</span></a></h4>
<p><code v-pre>timestamps</code> 方法创建具有可选精度（总位数）的 <code v-pre>created_at</code> 和 <code v-pre>updated_at</code> <code v-pre>TIMESTAMP</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamps</span><span class="token punctuation">(</span><span class="token variable">$precision</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="tinyincrements-collection-method" tabindex="-1"><a class="header-anchor" href="#tinyincrements-collection-method"><span><code v-pre>tinyIncrements()</code> {.collection-method}</span></a></h4>
<p><code v-pre>tinyIncrements</code> 方法创建一个自动递增的 <code v-pre>UNSIGNED TINYINT</code> 等效列作为主键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">tinyIncrements</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="tinyinteger-collection-method" tabindex="-1"><a class="header-anchor" href="#tinyinteger-collection-method"><span><code v-pre>tinyInteger()</code> {.collection-method}</span></a></h4>
<p><code v-pre>tinyInteger</code> 方法用于创建一个 <code v-pre>TINYINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">tinyInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="tinytext-collection-method" tabindex="-1"><a class="header-anchor" href="#tinytext-collection-method"><span><code v-pre>tinyText()</code> {.collection-method}</span></a></h4>
<p><code v-pre>tinyText</code> 方法用于创建一个 <code v-pre>TINYTEXT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">tinyText</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'notes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="unsignedbiginteger-collection-method" tabindex="-1"><a class="header-anchor" href="#unsignedbiginteger-collection-method"><span><code v-pre>unsignedBigInteger()</code> {.collection-method}</span></a></h4>
<p><code v-pre>unsignedBigInteger</code> 方法用于创建一个 <code v-pre>UNSIGNED BIGINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedBigInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="unsigneddecimal-collection-method" tabindex="-1"><a class="header-anchor" href="#unsigneddecimal-collection-method"><span><code v-pre>unsignedDecimal()</code> {.collection-method}</span></a></h4>
<p><code v-pre>unsignedDecimal</code> 方法用于创建一个 <code v-pre>UNSIGNED DECIMAL</code> 等效列，具有可选的精度（总位数）和小数位数（小数位数）：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedDecimal</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'amount'</span><span class="token punctuation">,</span> <span class="token variable">$precision</span> <span class="token operator">=</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token variable">$scale</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="unsignedinteger-collection-method" tabindex="-1"><a class="header-anchor" href="#unsignedinteger-collection-method"><span><code v-pre>unsignedInteger()</code> {.collection-method}</span></a></h4>
<p><code v-pre>unsignedInteger</code> 方法用于创建一个 <code v-pre>UNSIGNED INTEGER</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="unsignedmediuminteger-collection-method" tabindex="-1"><a class="header-anchor" href="#unsignedmediuminteger-collection-method"><span><code v-pre>unsignedMediumInteger()</code> {.collection-method}</span></a></h4>
<p><code v-pre>unsignedMediumInteger</code> 方法用于创建一个 <code v-pre>UNSIGNED MEDIUMINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedMediumInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="unsignedsmallinteger-collection-method" tabindex="-1"><a class="header-anchor" href="#unsignedsmallinteger-collection-method"><span><code v-pre>unsignedSmallInteger()</code> {.collection-method}</span></a></h4>
<p><code v-pre>unsignedSmallInteger</code> 方法用于创建一个 <code v-pre>UNSIGNED SMALLINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedSmallInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="unsignedtinyinteger-collection-method" tabindex="-1"><a class="header-anchor" href="#unsignedtinyinteger-collection-method"><span><code v-pre>unsignedTinyInteger()</code> {.collection-method}</span></a></h4>
<p><code v-pre>unsignedTinyInteger</code> 方法用于创建一个 <code v-pre>UNSIGNED TINYINT</code> 等效列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedTinyInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="ulidmorphs-collection-method" tabindex="-1"><a class="header-anchor" href="#ulidmorphs-collection-method"><span><code v-pre>ulidMorphs()</code> {.collection-method}</span></a></h4>
<p><code v-pre>ulidMorphs</code> 方法用于快速创建一个名称为 <code v-pre>{column}_id</code> ，类型为 <code v-pre>CHAR(26)</code> 的列和一个名称为 <code v-pre>{column}_type</code> ，类型为 <code v-pre>VARCHAR</code> 的列。</p>
<p>这个方法用于定义使用 UUID 标识符的<a href="https://learnku.com/docs/laravel/10.x/eloquent-relationships" target="_blank" rel="noopener noreferrer">多态关联</a>所需的列时使用。在下面的例子中，<code v-pre>taggable_id</code> 和 <code v-pre>taggable_type</code> 这两个列将会被创建：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">ulidMorphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="uuidmorphs-collection-method" tabindex="-1"><a class="header-anchor" href="#uuidmorphs-collection-method"><span><code v-pre>uuidMorphs()</code> {.collection-method}</span></a></h4>
<p><code v-pre>uuidMorphs</code> 方法用于快速创建一个名称为 <code v-pre>{column}_id</code> ，类型为 <code v-pre>CHAR(36)</code> 的列和一个名称为 <code v-pre>{column}_type</code> ，类型为 <code v-pre>VARCHAR</code> 的列。</p>
<p>这个方法用于定义使用 UUID 标识符的<a href="https://learnku.com/docs/laravel/10.x/eloquent-relationships" target="_blank" rel="noopener noreferrer">多态关联</a>所需的列时使用。在下面的例子中，<code v-pre>taggable_id</code> 和 <code v-pre>taggable_type</code> 这两个列将会被创建：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">uuidMorphs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taggable'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="ulid-collection-method" tabindex="-1"><a class="header-anchor" href="#ulid-collection-method"><span><code v-pre>ulid()</code> {.collection-method}</span></a></h4>
<p><code v-pre>ulid</code> 方法用于创建一个 <code v-pre>ULID</code> 类型的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">ulid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="uuid-collection-method" tabindex="-1"><a class="header-anchor" href="#uuid-collection-method"><span><code v-pre>uuid()</code> {.collection-method}</span></a></h4>
<p><code v-pre>uuid</code> 方法用于创建一个 <code v-pre>UUID</code> 类型的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">uuid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="year-collection-method" tabindex="-1"><a class="header-anchor" href="#year-collection-method"><span><code v-pre>year()</code> {.collection-method}</span></a></h4>
<p><code v-pre>year</code> 方法用于创建一个 <code v-pre>YEAR</code> 类型的列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">year</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'birth_year'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="字段修饰符" tabindex="-1"><a class="header-anchor" href="#字段修饰符"><span>字段修饰符</span></a></h3>
<p>除了上面列出的列类型外，在向数据库表添加列时还有几个可以使用的「修饰符」。例如，如果要把列设置为要使列为「可空」，你可以使用 <code v-pre>nullable</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">nullable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>下表时所有可用的列修饰符。此列表不包括<a href="#creating-indexes">索引修饰符</a>:</p>
<table>
<thead>
<tr>
<th>修饰符</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>-&gt;after('column')</code></td>
<td>将该列放在其它字段「之后」(MySQL)</td>
</tr>
<tr>
<td><code v-pre>-&gt;autoIncrement()</code></td>
<td>设置 INTEGER 类型的列为自动递增 (主键)</td>
</tr>
<tr>
<td><code v-pre>-&gt;charset('utf8mb4')</code></td>
<td>为该列指定字符集 (MySQL)</td>
</tr>
<tr>
<td><code v-pre>-&gt;collation('utf8mb4_unicode_ci')</code></td>
<td>为该列指定排序规则 (MySQL/PostgreSQL/SQL Server)</td>
</tr>
<tr>
<td><code v-pre>-&gt;comment('my comment')</code></td>
<td>为该列添加注释 (MySQL/PostgreSQL)</td>
</tr>
<tr>
<td><code v-pre>-&gt;default($value)</code></td>
<td>为该列指定一个「默认值」</td>
</tr>
<tr>
<td><code v-pre>-&gt;first()</code></td>
<td>将该列放在该表「首位」 (MySQL)</td>
</tr>
<tr>
<td><code v-pre>-&gt;from($integer)</code></td>
<td>设置自动递增字段的起始值 (MySQL / PostgreSQL)</td>
</tr>
<tr>
<td><code v-pre>-&gt;invisible()</code></td>
<td>使列对「SELECT *」查询不可见（MySQL）。</td>
</tr>
<tr>
<td><code v-pre>-&gt;nullable($value = true)</code></td>
<td>允许 NULL 值插入到该列</td>
</tr>
<tr>
<td><code v-pre>-&gt;storedAs($expression)</code></td>
<td>创建一个存储生成的列 (MySQL)</td>
</tr>
<tr>
<td><code v-pre>-&gt;unsigned()</code></td>
<td>设置 INTEGER 类型的字段为 UNSIGNED (MySQL)</td>
</tr>
<tr>
<td><code v-pre>-&gt;useCurrent()</code></td>
<td>设置 TIMESTAMP 类型的列使用 CURRENT_TIMESTAMP 作为默认值</td>
</tr>
<tr>
<td><code v-pre>-&gt;useCurrentOnUpdate()</code></td>
<td>将 TIMESTAMP 类型的列设置为在更新时使用 CURRENT_TIMESTAMP 作为新值</td>
</tr>
<tr>
<td><code v-pre>-&gt;virtualAs($expression)</code></td>
<td>创建一个虚拟生成的列 (MySQL)</td>
</tr>
<tr>
<td><code v-pre>-&gt;generatedAs($expression)</code></td>
<td>使用指定的序列选项创建标识列 (PostgreSQL)</td>
</tr>
<tr>
<td><code v-pre>-&gt;always()</code></td>
<td>定义序列值优先于标识列的输入 (PostgreSQL)</td>
</tr>
<tr>
<td><code v-pre>-&gt;isGeometry()</code></td>
<td>将空间列类型设置为 <code v-pre>geometry</code> - 默认类型为 <code v-pre>geography</code> (PostgreSQL)。</td>
</tr>
</tbody>
</table>
<h4 id="默认值表达式" tabindex="-1"><a class="header-anchor" href="#默认值表达式"><span>默认值表达式</span></a></h4>
<p><code v-pre>default</code> 修饰符接收一个变量或者一个 <code v-pre>\Illuminate\Database\Query\Expression</code> 实例。使用 <code v-pre>Expression</code> 实例可以避免使用包含在引号中的值，并且允许你使用特定数据库函数。这在当你需要给 <code v-pre>JSON</code> 字段指定默认值的时候特别有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Query<span class="token punctuation">\</span>Expression</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Migrations<span class="token punctuation">\</span>Migration</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token keyword">class</span> <span class="token class-name-definition class-name">extends</span> <span class="token class-name">Migration</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 运行迁移</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">up</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'flights'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'movies'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">default</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Expression</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'(JSON_ARRAY())'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">timestamps</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
支持哪些默认值的表示方式取决于你的数据库驱动、数据库版本、还有字段类型。请参考合适的文档使用。还有一点要注意的是，使用数据库特定函数，可能会将你绑牢到特定的数据库驱动上。</p>
</blockquote>
<h4 id="字段顺序" tabindex="-1"><a class="header-anchor" href="#字段顺序"><span>字段顺序</span></a></h4>
<p>使用 MySQL 数据库时，可以使用 <code v-pre>after</code> 方法在模式中的现有列后添加列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'password'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'address_line1'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'address_line2'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'city'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="修改字段" tabindex="-1"><a class="header-anchor" href="#修改字段"><span>修改字段</span></a></h3>
<p><code v-pre>change</code> 方法可以将现有的字段类型修改为新的类型或修改属性。比如，你可能想增加 <code v-pre>string</code> 字段的长度，可以使用 <code v-pre>change</code> 方法把 <code v-pre>name</code> 字段的长度从 25 增加到 50。所以，我们可以简单的更新字段属性然后调用 <code v-pre>change</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">change</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当修改一个列时，你必须明确包括所有你想在列定义上保留的修改器 —— 任何缺失的属性都将被丢弃。例如，为了保留 <code v-pre>unsigned</code>、<code v-pre>default</code> 和 <code v-pre>comment</code> 属性，你必须在修改列时明确每个属性的修改。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">integer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unsigned</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">default</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">comment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my comment'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">change</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="在-sqlite-上修改列" tabindex="-1"><a class="header-anchor" href="#在-sqlite-上修改列"><span>在 SQLite 上修改列</span></a></h4>
<p>如果应用程序使用的是 SQLite 数据库，请确保你已经通过 Composer 包管理器安装了 <code v-pre>doctrine/dbal</code> 包。Doctrine DBAL 库用于确定字段的当前状态，并创建对该字段进行指定调整所需的 SQL 查询：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> doctrine<span class="token operator">/</span>dbal</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你打算修改 <code v-pre>timestamp</code> 方法来创建列，你还需要将以下配置添加到应用程序的<code v-pre>config/database.php</code>配置文件中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>DBAL<span class="token punctuation">\</span>TimestampType</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token string single-quoted-string">'dbal'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'types'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'timestamp'</span> <span class="token operator">=></span> <span class="token class-name static-context">TimestampType</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
当使用 <code v-pre>doctrine/dbal</code> 包时，你可以修改以下列类型：<code v-pre>bigInteger</code>、<code v-pre>binary</code>、<code v-pre>boolean</code>、<code v-pre>char</code>、<code v-pre>date</code>、<code v-pre>dateTime</code>、<code v-pre>dateTimeTz</code>、<code v-pre>decimal</code>、<code v-pre>double</code>、<code v-pre>integer</code>、<code v-pre>json</code>、<code v-pre>longText</code>、<code v-pre>mediumText</code>、<code v-pre>smallInteger</code>、<code v-pre>string</code>、<code v-pre>text</code>、<code v-pre>time</code>、<code v-pre>tinyText</code>、<code v-pre>unsignedBigInteger</code>、<code v-pre>unsignedInteger</code>、<code v-pre>unsignedSmallInteger</code>、<code v-pre>ulid</code>、和 <code v-pre>uuid</code>。</p>
</blockquote>
<h4 id="重命名字段" tabindex="-1"><a class="header-anchor" href="#重命名字段"><span>重命名字段</span></a></h4>
<p>要重命名一个列，你可以使用模式构建器提供的 <code v-pre>renameColumn</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">renameColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'from'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'to'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="在较低版本数据库上重命名列" tabindex="-1"><a class="header-anchor" href="#在较低版本数据库上重命名列"><span>在较低版本数据库上重命名列</span></a></h4>
<p>如果你运行的数据库低于以下版本，你应该确保在重命名列之前通过 Composer 软件包管理器安装了 <code v-pre>doctrine/dbal</code> 库。</p>
<ul>
<li>MySQL &lt; <code v-pre>8.0.3</code></li>
<li>MariaDB &lt; <code v-pre>10.5.2</code></li>
<li>SQLite &lt; <code v-pre>3.25.0</code></li>
</ul>
<h3 id="删除字段" tabindex="-1"><a class="header-anchor" href="#删除字段"><span>删除字段</span></a></h3>
<p>要删除一个列，你可以使用 <code v-pre>dropColumn</code> 方法。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以传递一个字段数组给 <code v-pre>dropColumn</code> 方法来删除多个字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropColumn</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'avatar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'location'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="在较低版本的数据库中删除列的内容" tabindex="-1"><a class="header-anchor" href="#在较低版本的数据库中删除列的内容"><span>在较低版本的数据库中删除列的内容</span></a></h4>
<p>如果你运行的 SQLite 版本在 <code v-pre>3.35.0</code> 之前，你必须通过 Composer 软件包管理器安装 <code v-pre>doctrine/dbal</code> 包，然后才能使用 <code v-pre>dropColumn</code> 方法。不支持在使用该包时在一次迁移中删除或修改多个列。</p>
<h4 id="可用的命令别名" tabindex="-1"><a class="header-anchor" href="#可用的命令别名"><span>可用的命令别名</span></a></h4>
<p>Laravel 提供了几种常用的删除相关列的便捷方法。如下表所示：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;dropMorphs('morphable');</code></td>
<td>删除 <code v-pre>morphable_id</code> 和 <code v-pre>morphable_type</code> 字段</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropRememberToken();</code></td>
<td>删除 <code v-pre>remember_token</code> 字段</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropSoftDeletes();</code></td>
<td>删除 <code v-pre>deleted_at</code> 字段</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropSoftDeletesTz();</code></td>
<td><code v-pre>dropSoftDeletes()</code> 方法的别名</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropTimestamps();</code></td>
<td>删除 <code v-pre>created_at</code> 和 <code v-pre>updated_at</code> 字段</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropTimestampsTz();</code></td>
<td><code v-pre>dropTimestamps()</code> 方法别名</td>
</tr>
</tbody>
</table>
<h2 id="索引" tabindex="-1"><a class="header-anchor" href="#索引"><span>索引</span></a></h2>
<h3 id="创建索引" tabindex="-1"><a class="header-anchor" href="#创建索引"><span>创建索引</span></a></h3>
<p>结构生成器支持多种类型的索引。下面的例子中新建了一个值唯一的 <code v-pre>email</code> 字段。我们可以将 <code v-pre>unique</code> 方法链式地添加到字段定义上来创建索引：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token keyword type-declaration">string</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你也可以在定义完字段之后创建索引。为此，你应该调用结构生成器上的 <code v-pre>unique</code> 方法，此方法应该传入唯一索引的列名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你甚至可以将数组传递给索引方法来创建一个复合（或合成）索引：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">index</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'account_id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>创建索引时，Laravel 会自动生成一个合理的索引名称，但你也可以传递第二个参数来自定义索引名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'unique_email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="可用的索引类型" tabindex="-1"><a class="header-anchor" href="#可用的索引类型"><span>可用的索引类型</span></a></h4>
<p>Laravel 的结构生成器提供了 Laravel 支持的所有类型的索引方法。每个索引方法都接受一个可选的第二个参数来指定索引的名称。如果省略，名称将根据表和列的名称生成。下面是所有可用的索引方法：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;primary('id');</code></td>
<td>添加主键</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;primary(['id', 'parent_id']);</code></td>
<td>添加复合主键</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;unique('email');</code></td>
<td>添加唯一索引</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;index('state');</code></td>
<td>添加普通索引</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;fullText('body');</code></td>
<td>添加全文索引 (MySQL/PostgreSQL)</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;fullText('body')-&gt;language('english');</code></td>
<td>添加指定语言 (PostgreSQL) 的全文索引</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;spatialIndex('location');</code></td>
<td>添加空间索引（不支持 SQLite）</td>
</tr>
</tbody>
</table>
<h4 id="索引长度-mysql-mariadb" tabindex="-1"><a class="header-anchor" href="#索引长度-mysql-mariadb"><span>索引长度 &amp; MySQL / MariaDB</span></a></h4>
<p>默认情况下，Laravel 使用 <code v-pre>utf8mb4</code> 编码。如果你是在版本低于 5.7.7 的 MySQL 或者版本低于 10.2.2 的 MariaDB 上创建索引，那你就需要手动配置数据库迁移的默认字符串长度。也就是说，你可以通过在 <code v-pre>App\Providers\AppServiceProvider</code> 类的 <code v-pre>boot</code> 方法中调用 <code v-pre>Schema::defaultStringLength</code> 方法来配置默认字符串长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用程序「全局配置」</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">defaultStringLength</span><span class="token punctuation">(</span><span class="token number">191</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，你也可以选择开启数据库的 <code v-pre>innodb_large_prefix</code> 选项。至于如何正确开启，请自行查阅数据库文档。</p>
<h3 id="重命名索引" tabindex="-1"><a class="header-anchor" href="#重命名索引"><span>重命名索引</span></a></h3>
<p>若要重命名索引，你需要调用 <code v-pre>renameIndex</code> 方法。此方法接受当前索引名称作为其第一个参数，并将所需名称作为其第二个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">renameIndex</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'from'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'to'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
如果你的应用程序使用的是 SQLite 数据库，你必须通过 Composer 软件包管理器安装 <code v-pre>doctrine/dbal</code> 包，然后才能使用 <code v-pre>renameIndex</code> 方法。</p>
</blockquote>
<h3 id="删除索引" tabindex="-1"><a class="header-anchor" href="#删除索引"><span>删除索引</span></a></h3>
<p>若要删除索引，则必须指定索引的名称。Laravel 默认会自动将数据表名称、索引的字段名及索引类型简单地连接在一起作为名称。举例如下：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;dropPrimary('users_id_primary');</code></td>
<td>从「users」表中删除主键</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropUnique('users_email_unique');</code></td>
<td>从「users」表中删除 unique 索引</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropIndex('geo_state_index');</code></td>
<td>从「geo」表中删除基本索引</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropFullText('posts_body_fulltext');</code></td>
<td>从「post」表中删除一个全文索引</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;dropSpatialIndex('geo_location_spatialindex');</code></td>
<td>从「geo」表中删除空间索引（不支持 SQLite）</td>
</tr>
</tbody>
</table>
<p>如果将字段数组传给 <code v-pre>dropIndex</code> 方法，会删除根据表名、字段和键类型生成的索引名称。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'geo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropIndex</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'state'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 删除 'geo_state_index' 索引</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="外键约束" tabindex="-1"><a class="header-anchor" href="#外键约束"><span>外键约束</span></a></h3>
<p>Laravel 还支持创建用于在数据库层中的强制引用完整性的外键约束。例如，让我们在 <code v-pre>posts</code> 表上定义一个引用 <code v-pre>users</code> 表的 <code v-pre>id</code> 字段的 <code v-pre>user_id</code> 字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Schema<span class="token punctuation">\</span>Blueprint</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Schema</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">unsignedBigInteger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreign</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">references</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于这种外键约束的定义方式过于繁复，Laravel 额外提供了更简洁的方法，基于约定来提供更好的开发人员体验。当使用 <code v-pre>foreignId</code> 方法来创建列时，上面的示例还可以这么写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignId</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">constrained</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>foreignId</code> 方法是 <code v-pre>unsignedBigInteger</code> 的别名，而 <code v-pre>constrained</code> 方法将使用约定来确定所引用的表名和列名。如果表名与约定不匹配，可以通过将表名作为参数传递给 <code v-pre>constrained</code> 方法来指定表名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Blueprint</span> <span class="token variable">$table</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignId</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">constrained</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以为约束的「on delete」和「on update」属性指定所需的操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignId</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">constrained</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">onUpdate</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cascade'</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">onDelete</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cascade'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>还为这些操作提供了另一种表达性语法：</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>$table-&gt;cascadeOnUpdate();</code></td>
<td>更新应该级联</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;restrictOnUpdate();</code></td>
<td>应该限制更新</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;cascadeOnDelete();</code></td>
<td>删除应该级联</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;restrictOnDelete();</code></td>
<td>应该限制删除</td>
</tr>
<tr>
<td><code v-pre>$table-&gt;nullOnDelete();</code></td>
<td>删除应将外键值设置为空</td>
</tr>
</tbody>
</table>
<p>当使用任意 <a href="#column-modifiers">字段修饰符</a> 的时候，必须在调用 <code v-pre>constrained</code> 之前调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">foreignId</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">nullable</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">      <span class="token operator">-></span><span class="token function">constrained</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="删除外键" tabindex="-1"><a class="header-anchor" href="#删除外键"><span>删除外键</span></a></h4>
<p>要删除一个外键，你需要使用 <code v-pre>dropForeign</code> 方法，将要删除的外键约束作为参数传递。外键约束采用的命名方式与索引相同。即，将数据表名称和约束的字段连接起来，再加上 <code v-pre>_foreign</code> 后缀：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropForeign</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'posts_user_id_foreign'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>或者，可以给 <code v-pre>dropForeign</code> 方法传递一个数组，该数组包含要删除的外键的列名。数组将根据 Laravel 的 结构生成器使用的约束名称约定自动转换：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$table</span><span class="token operator">-></span><span class="token function">dropForeign</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="更改外键约束" tabindex="-1"><a class="header-anchor" href="#更改外键约束"><span>更改外键约束</span></a></h4>
<p>你可以在迁移文件中使用以下方法来开启或关闭外键约束：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">enableForeignKeyConstraints</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">disableForeignKeyConstraints</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Schema</span><span class="token operator">::</span><span class="token function">withoutForeignKeyConstraints</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 闭包中禁用的约束…</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：SQLite 默认禁用外键约束。使用 SQLite 时，请确保在数据库配置中<a href="https://learnku.com/docs/laravel/10.x/databasemd#configuration" target="_blank" rel="noopener noreferrer">启用外键支持</a> 然后再尝试在迁移中创建它们。另外，SQLite 只在创建表时支持外键，并且<a href="https://www.sqlite.org/omitted.html" target="_blank" rel="noopener noreferrer">将在修改表时不会支持</a>。</p>
</blockquote>
<h2 id="事件" tabindex="-1"><a class="header-anchor" href="#事件"><span>事件</span></a></h2>
<p>为方便起见，每个迁移操作都会派发一个 <a href="https://learnku.com/docs/laravel/10.x/events" target="_blank" rel="noopener noreferrer">事件</a>。以下所有事件都扩展了基础 <code v-pre>Illuminate\Database\Events\MigrationEvent</code> 类：</p>
<table>
<thead>
<tr>
<th>类</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code v-pre>Illuminate\Database\Events\MigrationsStarted</code></td>
<td>即将执行一批迁移。</td>
</tr>
<tr>
<td><code v-pre>Illuminate\Database\Events\MigrationsEnded</code></td>
<td>一批迁移已完成执行。</td>
</tr>
<tr>
<td><code v-pre>Illuminate\Database\Events\MigrationStarted</code></td>
<td>即将执行单个迁移。</td>
</tr>
<tr>
<td><code v-pre>Illuminate\Database\Events\MigrationEnded</code></td>
<td>单个迁移已完成执行。</td>
</tr>
<tr>
<td><code v-pre>Illuminate\Database\Events\SchemaDumped</code></td>
<td>数据库结构转储已完成。</td>
</tr>
<tr>
<td><code v-pre>Illuminate\Database\Events\SchemaLoaded</code></td>
<td>已加载现有数据库结构转储。</td>
</tr>
</tbody>
</table>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/migrations/14885" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/mi...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/migrations/14885" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/mi...</a></p>
</blockquote>
</div></template>


